package edu.nus.mtech.sem1.ca.plan;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import edu.nus.mtech.sem1.ca.cust.Status;
import edu.nus.mtech.sem1.ca.exception.DuplicateException;
import edu.nus.mtech.sem1.ca.plan.valueadd.ValueAddedService;
import edu.nus.mtech.sem1.ca.utils.Logger;
import edu.nus.mtech.sem1.ca.utils.TelephonyConstants;
import edu.nus.mtech.sem1.ca.utils.TelephonyUtils;


public class PlanDao {
	
	private String planId;
	private PlanType planType;
	private Set<ValueAddedService> valueAddedServices=new HashSet<ValueAddedService>();
	private Date dateCommenced;
	private Date dateTerminated;
	private String telephoneNumber;
	private Status subscriptionStatus;
	public PlanDao(){};
	
	public void addValueAddedService(ValueAddedService vaService) throws DuplicateException{
		
			ValueAddedService tempValueAddedService=getValueAddedServiceObjectIfExists(vaService);	
			Logger.log("Temp Value added service :: "+tempValueAddedService);
			if (tempValueAddedService==null){
				//Remember, duplicate vaServices will be GONE !!!
				boolean addSuccessful=this.valueAddedServices.add(vaService);
				Logger.log("Value Added Service Add Successful?? ::: "+addSuccessful);
			}
			else{
				System.err.println(TelephonyConstants.ERR_DUPLICATE_VALUE_ADDED_SERVICE);
				throw new DuplicateException(TelephonyConstants.ERR_DUPLICATE_VALUE_ADDED_SERVICE);
			}
	}

	//Made public for Testing purposes
	/**
	 * Returns a ValueAddedService object if it exists. Else, returns null
	 * @param vaService
	 * @return
	 */
	public ValueAddedService getValueAddedServiceObjectIfExists(ValueAddedService vaService) {
		ValueAddedService returnValueAddedService=null;
		
			for (ValueAddedService eachValueAddedService:valueAddedServices){
				if (eachValueAddedService.getValueAddedId().equals(vaService.getValueAddedId())){
					returnValueAddedService=eachValueAddedService;
					break;
				}
		}
		return returnValueAddedService;
	}

	public PlanType getPlanType() {
		return planType;
	}

	public void setPlanType(PlanType planType) {
		this.planType = planType;
	}

	public Set<ValueAddedService> getValueAddedServices() {
		return valueAddedServices;
	}

	public Date getDateCommenced() {
		return dateCommenced;
	}

	public void setDateCommenced(Date dateCommenced) {
		this.dateCommenced = dateCommenced;
	}

	public Date getDateTerminated() {
		return dateTerminated;
	}

	public void setDateTerminated(Date dateTerminated) {
		this.dateTerminated = dateTerminated;
	}

	public String getTelephoneNumber() {
		return telephoneNumber;
	}

	public void setTelephoneNumber(String telephoneNumber) {
		this.telephoneNumber = telephoneNumber;
	}

	public Status getSubscriptionStatus() {
		return subscriptionStatus;
	}

	public void setSubscriptionStatus(Status subscriptionStatus) {
		this.subscriptionStatus = subscriptionStatus;
	}


	@Override
	public boolean equals(Object planObj) {
		boolean allResult=false;
		
		if (planObj!=null && planObj instanceof Plan){
			PlanDao incomingPlan=(PlanDao)planObj;
			
			//Plan id is the key to the plan. All others doesnt matter
			if (incomingPlan.getPlanId().equals(planId)){
				allResult=true;
			}
			
		/*	
			if (incomingPlan.getNumber().equals(number)){
				allResult=true;
			}
			if (incomingPlan.getPlanType().equals(planType)){
				allResult=allResult && true;
			}
			if (incomingPlan.getValueAddedServices().equals(valueAddedServices)){
				allResult=allResult && true;
			}
			if (incomingPlan.getDateCommenced().equals(dateCommenced)){
				allResult=allResult && true;
			}
			if (incomingPlan.getDateTerminated().equals(dateTerminated)){
				allResult=allResult && true;
			}
			if (incomingPlan.getTelephoneNumber().equals(telephoneNumber)){
				allResult=allResult && true;
			}
			if (incomingPlan.getSubscriptionStatus().equals(subscriptionStatus)){
				allResult=allResult && true;
			}*/
			
		}
		return allResult;
	}
	
	@Override
	public int hashCode() {
		
		//Had to override the hashcode method because we have overridden the equals method.
		// Just multiplying with a good primary number to avoid false duplicates
		int hashCode=(TelephonyUtils.getHashCode(planId)*73)+TelephonyUtils.getHashCode(telephoneNumber)+TelephonyUtils.getHashCode(planType)+TelephonyUtils.getHashCode(valueAddedServices)+TelephonyUtils.getHashCode(dateCommenced)+TelephonyUtils.getHashCode(dateTerminated)+TelephonyUtils.getHashCode(telephoneNumber)+TelephonyUtils.getHashCode(subscriptionStatus);
		return hashCode;
		
	}
	
	@Override
	public String toString() {
		return(planId+"::"+telephoneNumber+"::"+planType+"::"+valueAddedServices+"::"+dateCommenced+"::"+dateTerminated+"::"+telephoneNumber+"::"+subscriptionStatus);
	}

	public String getPlanId() {
		return planId;
	}

	public void setPlanId(String planId) {
		this.planId = planId;
	}
	
}
